﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using SHFST.Core;
using SHFST.Agents;

namespace SHFST.Config
{
   class LevelImporter
   {
      public void read (string fileName,
                        ContentManager content,
                        GraphicsDevice device,
                        ref int levelSize,
                        ref ScrollingBackground background,
                        ref Salmon salmon,
                        ref List<Enemy> enemies,
                        ref List<InfoBox> infoBoxes)
      {
         StreamReader reader = new StreamReader (fileName);

         String line;
         String [] newVals = new String [3];
         String tempString;
         Point tempPoint = new Point ();

         while (!reader.EndOfStream)
         {
            line = reader.ReadLine ();
            if (!line.Contains ("//") && line.Length > 0)
            {
               if (line.Contains ("Background"))
               {
                  tempString = ".\\Textures\\Core\\" + line;
                  levelSize = Convert.ToInt32 (reader.ReadLine ());

                  background.Load (device, content.Load<Texture2D> (tempString));

                  int screenHeight = device.Viewport.Height;
                  Bullet bullet = new Bullet ();
                  bullet.Load (content.Load<Texture2D> (".\\Textures\\Agents\\Bullet"),
                               new Point (device.Viewport.Width, screenHeight));
                  salmon.Load (content,
                               content.Load<Texture2D> (".\\Textures\\Agents\\Salmon"),
                               new Point (138, 101),
                               new Point (1, 1),//new Point (8, 1),
                               new Point (100, screenHeight * 2 / 3),
                               screenHeight,
                               bullet);
               }
               else if (line.Length > 0 && line.Split (' ').Length == 1)
               {
                  int life = 1;
                  Texture2D newTex = content.Load<Texture2D> (".\\Textures\\Agents\\" + line);
                  Point frameSize = new Point (newTex.Width, newTex.Height);
                  Point sheetSize = new Point (1, 1);

                  if (line == "Bag")
                  {
                     frameSize = new Point (115, 100);
                     //sheetSize = new Point (4, 1);
                  }
                  if (line == "Tire")
                  {
                     life = 2;
                     frameSize = new Point (84, 100);
                     //sheetSize = new Point (6, 1);
                  }
                  else if (line == "Bear")
                  {
                     life = 4;
                  }
                  else if (line == "Fisher")
                  {
                     life = 5;
                  }

                  line = reader.ReadLine ();
                  newVals = line.Split (' ');
                  tempPoint.X = Convert.ToInt32 (newVals[0]);
                  tempPoint.Y = Convert.ToInt32 (newVals[1]);

                  Enemy newEnemy = new Enemy ();
                  newEnemy.Load (content, newTex, frameSize, sheetSize, tempPoint, life);
                  enemies.Add (newEnemy);
               }
               else if (line.Length > 0)
               {
                  tempString = line;
                  line = reader.ReadLine ();
                  newVals = line.Split (' ');
                  tempPoint.X = Convert.ToInt32 (newVals[0]);
                  tempPoint.Y = Convert.ToInt32 (newVals[1]);

                  InfoBox newBox = new InfoBox ();
                  newBox.Load (content.Load<SpriteFont> (".\\Fonts\\HUD"), tempString, tempPoint);
                  infoBoxes.Add (newBox);
               }
            }
         }

         reader.Close ();
      }
   }
}
